// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ResultSegmentWrapper.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Defines the ResultSegmentWrapper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureContrib.WindowsAzure.StorageClient
{
	internal class ResultSegmentWrapper<T, TContract> : IResultSegment<TContract>
	{
		private readonly ResultSegment<T> resultSegment;

		private readonly Converter<T, TContract> converter;

		public ResultSegmentWrapper(ResultSegment<T> resultSegment, Converter<T, TContract> converter)
		{
			this.resultSegment = resultSegment;
			this.converter = converter;
		}

		public ResultContinuation ContinuationToken
		{
			get
			{
				return resultSegment.ContinuationToken;
			}
		}

		public bool HasMoreResults
		{
			get
			{
				return resultSegment.HasMoreResults;
			}
		}

		public IEnumerable<TContract> Results
		{
			get
			{
				return resultSegment.Results.Select(rs => converter(rs));
			}
		}

		public IResultSegment<TContract> GetNext
		{
			get
			{
				var getNext = resultSegment.GetNext();
				return new ResultSegmentWrapper<T, TContract>(getNext, converter);
			}
		}

		public IAsyncResult BeginGetNext(AsyncCallback callback, object state)
		{
			return resultSegment.BeginGetNext(callback, state);
		}

		public IResultSegment<TContract> EndGetNext(IAsyncResult asyncResult)
		{
			var endGetNext = resultSegment.EndGetNext(asyncResult);
			return new ResultSegmentWrapper<T, TContract>(endGetNext, converter);
		}
	}
}